package cn.lichenes.utils;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;


public class Utils {
	//验证码
	public static String getVerification() {
		String str = "";
            Random random = new Random();
            for (int i = 0; i < 4; i++) {
                str += random.nextInt(10);
            }
			return str;
	}
	//时间
	public static String getDate() {
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
		Date date = new Date();
		String str = sdf.format(date);
		return str;
	}
	//MD5加密
	public static String getMd5(String password) {
		StringBuffer sb = null;
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			byte[] digest = messageDigest.digest(password.getBytes());
			sb = new StringBuffer();
			for (byte b : digest) {
				int i = b & 0xff;
				String hexString = Integer.toHexString(i);
				if (hexString.length() < 2) {
					hexString = "0" + hexString;
				}
				sb.append(hexString);
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	//UUID
	public static String getUUID(){
		String uuid = UUID.randomUUID().toString().replace("-", "");
		return uuid;
		}
	
	public static void deletePicture(String address,String path) throws Exception{
		String[] strings = address.split("/");
		String realPath = path + "\\\\" + strings[strings.length - 1];
		File file = new File(realPath);
		if (file.isFile()) {
			file.delete();
		}
	}
}
